home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmpwalk_asy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-19  |  6.3 KB  |  232 lines

  1. /*
  2.  * snmpwalk.c - send snmp GETNEXT requests to a network entity, walking a subtree.
  3.  * This uses the asynchronous interface directly.
  4.  *
  5.  */
  6. /***********************************************************
  7.     Copyright 1989 by Carnegie Mellon University
  8.  
  9.                       All Rights Reserved
  10.  
  11. Permission to use, copy, modify, and distribute this software and its 
  12. documentation for any purpose and without fee is hereby granted, 
  13. provided that the above copyright notice appear in all copies and that
  14. both that copyright notice and this permission notice appear in 
  15. supporting documentation, and that the name of CMU not be
  16. used in advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.  
  18.  
  19. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  20. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  21. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  23. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  24. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  25. SOFTWARE.
  26. ******************************************************************/
  27. #include <sys/param.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <netdb.h>
  33. #include <stdio.h>
  34. #include <ctype.h>
  35. #include <sys/time.h>
  36. #include <errno.h>
  37.  
  38. #include "snmp.h"
  39. #include "snmp_impl.h"
  40. #include "asn1.h"
  41. #include "snmp_api.h"
  42. #include "snmp_client.h"
  43.  
  44. #ifndef BSD4_3
  45. #define BSD4_2
  46. #endif
  47.  
  48. #ifndef BSD4_3
  49.  
  50. typedef long    fd_mask;
  51. #define NFDBITS    (sizeof(fd_mask) * NBBY)    /* bits per mask */
  52.  
  53. #define    FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
  54. #define    FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
  55. #define    FD_ISSET(n, p)    ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
  56. #define FD_ZERO(p)    bzero((char *)(p), sizeof(*(p)))
  57. #endif
  58.  
  59. oid objid_mib[] = {1, 3, 6, 1, 2, 1};
  60.  
  61. extern int  errno;
  62. int    snmp_dump_packet = 0;
  63. struct state {
  64.     oid    name[MAX_NAME_LEN];
  65.     int name_length;
  66.     oid root[MAX_NAME_LEN];
  67.     int    rootlen;
  68.     int running;
  69.     int waiting;
  70. } state_info;
  71.  
  72. snmp_input(op,  session, reqid, pdu, magic)
  73.     int op;
  74.     struct snmp_session *session;
  75.     int reqid;
  76.     struct snmp_pdu *pdu;
  77.     void *magic;
  78. {
  79.     struct variable_list *vars;
  80.     struct state *state = (struct state *)magic;
  81.     int count;
  82.  
  83.     state->waiting = 0;
  84.     state->running = 0;
  85.     if (op == RECEIVED_MESSAGE && pdu->command == GET_RSP_MSG){
  86.     if (pdu->errstat == SNMP_ERR_NOERROR){
  87.         for(vars = pdu->variables; vars; vars = vars->next_variable){
  88.         if (vars->name_length < state->rootlen || bcmp(state->root, vars->name, state->rootlen * sizeof(oid)))
  89.             continue;    /* not part of this subtree */
  90.         print_variable(vars->name, vars->name_length, vars);
  91.         bcopy((char *)vars->name, (char *)state->name, vars->name_length * sizeof(oid));
  92.         state->name_length = vars->name_length;
  93.         state->running = 1; /* restart so we can get next variable */
  94.         }
  95.     } else {
  96.         if (pdu->errstat == SNMP_ERR_NOSUCHNAME){
  97.         printf("End of MIB.\n");
  98.         } else {
  99.         printf("Error in packet.\nReason: %s\n", snmp_errstring(pdu->errstat));
  100.         if (pdu->errstat == SNMP_ERR_NOSUCHNAME){
  101.             printf("The request for this object identifier failed: ");
  102.             for(count = 1, vars = pdu->variables; vars && count != pdu->errindex;
  103.             vars = vars->next_variable, count++)
  104.                 ;
  105.             if (vars)
  106.             print_objid(vars->name, vars->name_length);
  107.             printf("\n");
  108.         }
  109.         }
  110.     }
  111.     } else if (op == TIMED_OUT){
  112.     /* We don't restart on timeout so main will exit */
  113.     printf("Timed Out\n");
  114.     }
  115.     return 1;
  116. }
  117.  
  118. main(argc, argv)
  119.     int        argc;
  120.     char    *argv[];
  121. {
  122.     struct snmp_session    session, *ss;
  123.     struct snmp_pdu *pdu;
  124.     int    arg;
  125.     char *gateway = NULL;
  126.     char *community = NULL;
  127.     int    count, numfds, gotroot = 0, block;
  128.     fd_set fdset;
  129.     struct timeval timeout, *tvp;
  130.     struct state *state = &state_info;
  131.  
  132.     init_mib();
  133.     /*
  134.      * usage: snmpwalk gateway-name community-name [object-id]
  135.      */
  136.     for(arg = 1; arg < argc; arg++){
  137.     if (argv[arg][0] == '-'){
  138.         switch(argv[arg][1]){
  139.         case 'd':
  140.             snmp_dump_packet++;
  141.             break;
  142.         default:
  143.             printf("invalid option: -%c\n", argv[arg][1]);
  144.             break;
  145.         }
  146.         continue;
  147.     }
  148.     if (gateway == NULL){
  149.         gateway = argv[arg];
  150.     } else if (community == NULL){
  151.         community = argv[arg]; 
  152.     } else {
  153.         state->rootlen = MAX_NAME_LEN;
  154.         if (read_objid(argv[arg], state->root, &state->rootlen)){
  155.         gotroot = 1;
  156.         } else {
  157.         printf("Invalid object identifier: %s\n", argv[arg]);
  158.         }
  159.     }
  160.     }
  161.  
  162.     if (gotroot == 0){
  163.     bcopy((char *)objid_mib, (char *)state->root, sizeof(objid_mib));
  164.     state->rootlen = sizeof(objid_mib) / sizeof(oid);
  165.     gotroot = 1;
  166.     }
  167.  
  168.     if (!(gateway && community && gotroot == 1)){
  169.     printf("usage: snmpwalk gateway-name community-name object-identifier\n");
  170.     exit(1);
  171.     }
  172.  
  173.     session.peername = gateway;
  174.     session.community = (u_char *)community;
  175.     session.community_len = strlen((char *)community);
  176.     session.retries = SNMP_DEFAULT_RETRIES;
  177.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  178.     session.authenticator = NULL;
  179.     session.callback = snmp_input;
  180.     session.callback_magic = (void *)state;
  181.     ss = snmp_open(&session);
  182.     if (ss == NULL){
  183.     printf("Couldn't open snmp\n");
  184.     exit(-1);
  185.     }
  186.  
  187.     state->running = 1;
  188.     state->waiting = 0;
  189.     bcopy((char *)state->root, (char *)state->name, state->rootlen * sizeof(oid));
  190.     state->name_length = state->rootlen;
  191.  
  192.     while(state->running){
  193.     if (!state->waiting){
  194.         pdu = snmp_pdu_create(GETNEXT_REQ_MSG);
  195.  
  196.         snmp_add_null_var(pdu, state->name, state->name_length);
  197.  
  198.         if (snmp_send(ss, pdu) == 0){
  199.         snmp_free_pdu(pdu);
  200.         state->running = 0;
  201.         }
  202.     }
  203.     state->waiting = 1;    
  204.  
  205.     numfds = 0;
  206.     FD_ZERO(&fdset);
  207.     block = 1;
  208.     tvp = &timeout;
  209.     timerclear(tvp);
  210.     snmp_select_info(&numfds, &fdset, tvp, &block);
  211.     if (block == 1)
  212.         tvp = NULL;    /* block without timeout */
  213.     count = select(numfds, &fdset, 0, 0, tvp);
  214.     if (count > 0){
  215.         snmp_read(&fdset);
  216.     } else switch(count){
  217.         case 0:
  218.         snmp_timeout();
  219.         break;
  220.         case -1:
  221.         if (errno == EINTR){
  222.             continue;
  223.         } else {
  224.             perror("select");
  225.         }
  226.         default:
  227.         printf("select returned %d\n", count);
  228.     }
  229.     }
  230. }
  231.  
  232.